#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Qa Up Down Convertion
# Generated: Thu Jan  3 15:55:53 2013
##################################################

execfile("down_converter.py")
execfile("up_converter.py")

from PyQt4 import Qt
from gnuradio import digital
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from gnuradio.qtgui import qtgui
from optparse import OptionParser
import numpy
import sip
import sys

class qa_up_down_convertion(gr.top_block, Qt.QWidget):

	def __init__(self):
		gr.top_block.__init__(self, "Qa Up Down Convertion")
		Qt.QWidget.__init__(self)
		self.setWindowTitle("Qa Up Down Convertion")
		self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
		self.top_scroll_layout = Qt.QVBoxLayout()
		self.setLayout(self.top_scroll_layout)
		self.top_scroll = Qt.QScrollArea()
		self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
		self.top_scroll_layout.addWidget(self.top_scroll)
		self.top_scroll.setWidgetResizable(True)
		self.top_widget = Qt.QWidget()
		self.top_scroll.setWidget(self.top_widget)
		self.top_layout = Qt.QVBoxLayout(self.top_widget)
		self.top_grid_layout = Qt.QGridLayout()
		self.top_layout.addLayout(self.top_grid_layout)


		##################################################
		# Variables
		##################################################
		self.samp_rate = samp_rate = 32000

		##################################################
		# Blocks
		##################################################
		self.tab = Qt.QTabWidget()
		self.tab_widget_0 = Qt.QWidget()
		self.tab_layout_0 = Qt.QBoxLayout(Qt.QBoxLayout.TopToBottom, self.tab_widget_0)
		self.tab_grid_layout_0 = Qt.QGridLayout()
		self.tab_layout_0.addLayout(self.tab_grid_layout_0)
		self.tab.addTab(self.tab_widget_0, "Generated Signal")
		self.tab_widget_1 = Qt.QWidget()
		self.tab_layout_1 = Qt.QBoxLayout(Qt.QBoxLayout.TopToBottom, self.tab_widget_1)
		self.tab_grid_layout_1 = Qt.QGridLayout()
		self.tab_layout_1.addLayout(self.tab_grid_layout_1)
		self.tab.addTab(self.tab_widget_1, "Up Converted Signal")
		self.tab_widget_2 = Qt.QWidget()
		self.tab_layout_2 = Qt.QBoxLayout(Qt.QBoxLayout.TopToBottom, self.tab_widget_2)
		self.tab_grid_layout_2 = Qt.QGridLayout()
		self.tab_layout_2.addLayout(self.tab_grid_layout_2)
		self.tab.addTab(self.tab_widget_2, "Down Converted Signal")
		self.top_layout.addWidget(self.tab)
		self.up_converter_0 = up_converter(
			samp_rate=32e3,
			base_freq=5e3,
		)
		self.random_source_x_0 = gr.vector_source_b(map(int, numpy.random.randint(0, 255, 1000)), True)
		self.qtgui_sink_x_0_1 = qtgui.sink_c(
			1024, #fftsize
			firdes.WIN_BLACKMAN_hARRIS, #wintype
			0, #fc
			samp_rate, #bw
			"QT GUI Plot", #name
			True, #plotfreq
			True, #plotwaterfall
			True, #plottime
			True, #plotconst
		)
		self._qtgui_sink_x_0_1_win = sip.wrapinstance(self.qtgui_sink_x_0_1.pyqwidget(), Qt.QWidget)
		self.tab_layout_2.addWidget(self._qtgui_sink_x_0_1_win)
		self.qtgui_sink_x_0_0 = qtgui.sink_f(
			1024, #fftsize
			firdes.WIN_BLACKMAN_hARRIS, #wintype
			0, #fc
			samp_rate, #bw
			"QT GUI Plot", #name
			True, #plotfreq
			True, #plotwaterfall
			True, #plottime
			True, #plotconst
		)
		self._qtgui_sink_x_0_0_win = sip.wrapinstance(self.qtgui_sink_x_0_0.pyqwidget(), Qt.QWidget)
		self.tab_layout_1.addWidget(self._qtgui_sink_x_0_0_win)
		self.qtgui_sink_x_0 = qtgui.sink_c(
			1024, #fftsize
			firdes.WIN_BLACKMAN_hARRIS, #wintype
			0, #fc
			samp_rate, #bw
			"QT GUI Plot", #name
			True, #plotfreq
			True, #plotwaterfall
			True, #plottime
			True, #plotconst
		)
		self._qtgui_sink_x_0_win = sip.wrapinstance(self.qtgui_sink_x_0.pyqwidget(), Qt.QWidget)
		self.tab_layout_0.addWidget(self._qtgui_sink_x_0_win)
		self.gr_throttle_0 = gr.throttle(gr.sizeof_gr_complex*1, samp_rate)
		self.down_converter_0 = down_converter(
			cutoff_freq=2.5e3,
			base_freq=5e3,
			samp_rate=32e3,
			beta=3.4,
		)
		self.digital_qam_mod_0 = digital.qam.qam_mod(
		  constellation_points=4,
		  mod_code="gray",
		  differential=True,
		  samples_per_symbol=16,
		  excess_bw=0.35,
		  verbose=False,
		  log=False,
		  )

		##################################################
		# Connections
		##################################################
		self.connect((self.down_converter_0, 0), (self.qtgui_sink_x_0_1, 0))
		self.connect((self.random_source_x_0, 0), (self.digital_qam_mod_0, 0))
		self.connect((self.digital_qam_mod_0, 0), (self.gr_throttle_0, 0))
		self.connect((self.gr_throttle_0, 0), (self.qtgui_sink_x_0, 0))
		self.connect((self.gr_throttle_0, 0), (self.up_converter_0, 0))
		self.connect((self.up_converter_0, 0), (self.down_converter_0, 0))
		self.connect((self.up_converter_0, 0), (self.qtgui_sink_x_0_0, 0))

	def get_samp_rate(self):
		return self.samp_rate

	def set_samp_rate(self, samp_rate):
		self.samp_rate = samp_rate
		self.qtgui_sink_x_0.set_frequency_range(0, self.samp_rate)
		self.qtgui_sink_x_0_0.set_frequency_range(0, self.samp_rate)
		self.qtgui_sink_x_0_1.set_frequency_range(0, self.samp_rate)

if __name__ == '__main__':
	parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
	(options, args) = parser.parse_args()
	qapp = Qt.QApplication(sys.argv)
	tb = qa_up_down_convertion()
	tb.start()
	tb.show()
	qapp.exec_()
	tb.stop()

